/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ConsultarFormaPagamentoGUI.java
 *
 * Created on 26/07/2011, 22:07:47
 */

package carrancao.gui;

import carrancao.controlador.Fachada;
import carrancao.entidades.Cliente;
import carrancao.entidades.FormaPagamento;
import carrancao.util.FormaPagamentoTableModel;
import java.awt.Cursor;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author Lubnnia
 */
public class ConsultarFormaPagamentoGUI extends javax.swing.JFrame {

    private List<FormaPagamento> listFormas = new ArrayList<FormaPagamento>();
    private FormaPagamentoTableModel formaTableModel;

    /** Creates new form ConsultarFormaPagamentoGUI */
    public ConsultarFormaPagamentoGUI() {
        initComponents();
        setLocationRelativeTo(null);
        refreshTable(listFormas);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroupPesquisa = new javax.swing.ButtonGroup();
        jTextFieldCampoConsulta = new javax.swing.JTextField();
        jRadioButtonCodigo = new javax.swing.JRadioButton();
        jRadioButtonNome = new javax.swing.JRadioButton();
        jButtonPesquisar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTableFormaPagamento = new javax.swing.JTable();
        jButtonFechar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Consultar Formas de Pagamento");

        jTextFieldCampoConsulta.setFont(new java.awt.Font("Tahoma", 0, 12));

        buttonGroupPesquisa.add(jRadioButtonCodigo);
        jRadioButtonCodigo.setFont(new java.awt.Font("Tahoma", 0, 12));
        jRadioButtonCodigo.setText("Código");

        buttonGroupPesquisa.add(jRadioButtonNome);
        jRadioButtonNome.setFont(new java.awt.Font("Tahoma", 0, 12));
        jRadioButtonNome.setSelected(true);
        jRadioButtonNome.setText("Nome");

        jButtonPesquisar.setFont(new java.awt.Font("Tahoma", 0, 14));
        jButtonPesquisar.setText("Pesquisar");
        jButtonPesquisar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonPesquisarActionPerformed(evt);
            }
        });

        jTableFormaPagamento.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jTableFormaPagamento.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTableFormaPagamentoMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jTableFormaPagamentoMouseEntered(evt);
            }
        });
        jScrollPane1.setViewportView(jTableFormaPagamento);

        jButtonFechar.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jButtonFechar.setText("Fechar");
        jButtonFechar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonFecharActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 501, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(jTextFieldCampoConsulta, javax.swing.GroupLayout.PREFERRED_SIZE, 238, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jRadioButtonCodigo)
                                .addGap(18, 18, 18)
                                .addComponent(jRadioButtonNome)
                                .addGap(18, 18, 18)
                                .addComponent(jButtonPesquisar))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(146, 146, 146)
                        .addComponent(jButtonFechar, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldCampoConsulta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jRadioButtonCodigo)
                    .addComponent(jRadioButtonNome)
                    .addComponent(jButtonPesquisar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(81, 81, 81)
                .addComponent(jButtonFechar, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(58, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void limparCampos() {
        jTextFieldCampoConsulta.setText("");
    }

    private void refreshTable(List<FormaPagamento> list) {
        formaTableModel = new FormaPagamentoTableModel(list);
        jTableFormaPagamento.setModel(formaTableModel);
    }

    private void jButtonFecharActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonFecharActionPerformed
        dispose();
    }//GEN-LAST:event_jButtonFecharActionPerformed

    private void jTableFormaPagamentoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableFormaPagamentoMouseClicked
        if(evt.getClickCount() == 2) {
             int linha = jTableFormaPagamento.getSelectedRow();

             AlterarFormaPagamentoGUI tela = new AlterarFormaPagamentoGUI();
             tela.setFormaPagamentos(formaTableModel.getFormaPagamento(linha));
             tela.setVisible(true);
        }
    }//GEN-LAST:event_jTableFormaPagamentoMouseClicked

    private void jTableFormaPagamentoMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableFormaPagamentoMouseEntered
        jTableFormaPagamento.setCursor(new Cursor(Cursor.HAND_CURSOR));
    }//GEN-LAST:event_jTableFormaPagamentoMouseEntered

    private void jButtonPesquisarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonPesquisarActionPerformed
        try {
            if(jRadioButtonCodigo.isSelected()) {
                FormaPagamento forma = null;
                forma = Fachada.getInstance().getFormaControl().pesquisarFormaPagamentoPorID(Integer.parseInt(jTextFieldCampoConsulta.getText()));

                if(forma != null) {
                    listFormas.add(forma);
                    refreshTable(listFormas);;
                } else {
                    JOptionPane.showMessageDialog(null, "Código inválido!", "Mensagem do Sistema", JOptionPane.ERROR_MESSAGE);
                }
                limparCampos();

          } else if(jRadioButtonNome.isSelected()) {

              listFormas = Fachada.getInstance().getFormaControl().pesquisarFormaPagamentoPorNome(jTextFieldCampoConsulta.getText());

              if(!(listFormas.isEmpty())) {
                  refreshTable(listFormas);
              } else {
                  JOptionPane.showMessageDialog(null, "Esta forma de pagamento não existe!", "Mensagem do Sistema", JOptionPane.ERROR_MESSAGE);
              }
              limparCampos();
          }

        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Nenhuma forma de pagamento encontrada!", "Mensagem do Sistema", JOptionPane.INFORMATION_MESSAGE);
        }
    }//GEN-LAST:event_jButtonPesquisarActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ConsultarFormaPagamentoGUI().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroupPesquisa;
    private javax.swing.JButton jButtonFechar;
    private javax.swing.JButton jButtonPesquisar;
    private javax.swing.JRadioButton jRadioButtonCodigo;
    private javax.swing.JRadioButton jRadioButtonNome;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTableFormaPagamento;
    private javax.swing.JTextField jTextFieldCampoConsulta;
    // End of variables declaration//GEN-END:variables

}
